import React, { Component } from 'react'
import {ListBox} from '../../../components'
import { Modal } from 'antd';
import {fetchData} from '../../../components/AjaxConsumer'
import AddPage from './addPage'
import UpdatePage from './UpdatePage'
import UpdatePageNo from './UpdatePageNo'
import CheckPage from './CheckPage'
import CheckAlonePage from './CheckAlonePage'
import PrintPage from './PrintPage'

const confirm = Modal.confirm;

export default class ListProHandOutPage extends Component{
    constructor(props) {
        super(props);
        //行操作列表-已弃用
        this.rowOperation = [
            
        ];
        //header 操作列表111
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '创建',
                iconClass: 'plus',
                permission: 'prohandout-create'
            },
            {
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit',
                permission: 'handout-edit'
            },
            {
                handleName: 'editNo',
                showName: '按单修改',
                iconClass: 'edit',
                permission: 'handout-edit-no'
            },
            // {
            //     handleName: 'check',
            //     showName: '按单审核',
            //     iconClass: 'eye',
            //     permission: 'handout-audit'
            // },
            // {
            //     handleName: 'checkAlone',
            //     showName: '审核',
            //     iconClass: 'eye',
            //     permission: 'handout-audit-alone'
            // },
            {
                handleName: 'print',
                showName: '打印',
                iconClass: 'print',
                permission: 'handout-print'
            },{
                handleName: 'toSell',
                showName: '转手工',
                iconClass: 'toSell',
                permission: 'handout-toSell'
            }

        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        // foreverHidden永久隐藏
        this.tableColumn = {
            storageKey: 'handOutId',
            data: [
                {
                    showName: 'id',
                    columnName: 'handOutId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '手工出库单号',
                    columnName: 'handOutNo',
                    columnWidth: 130
                },
                {
                    showName: '型号',
                    columnName: 'model',
                    columnWidth: 120
                },
                {
                    showName: '品名',
                    columnName: 'productName',
                    columnWidth: 120
                },
                {
                    showName: '定量',
                    columnName: 'density',
                    columnWidth: 100
                },
                {
                    showName: '规格',
                    columnName: 'spec',
                    columnWidth: 100
                },
                // {
                //     showName: '类型',
                //     columnName: 'type',
                //     columnWidth: 100,
                //     showOption: [
                //         {
                //             optionKey: '0',
                //             optionShow: '正品'
                //         },{
                //             optionKey: '1',
                //             optionShow: '样品'
                //         }
                //     ]
                // },
                {
                    showName: '等级',
                    columnName: 'grade',
                    showOption: [
                        {
                            optionKey: '1',
                            optionShow: '一等品'
                        },{
                            optionKey: '2',
                            optionShow: '二等品'
                        },{
                            optionKey: '3',
                            optionShow: '处理品'
                        }
                    ]
                }, 
                {
                    showName: '级别',
                    columnName: 'level'
                },{
                    showName: '仓库',
                    columnName: 'waCode',
                    showOption: []
                },
                {
                    showName: '需求数量',
                    columnName: 'requireAmount',
                    columnWidth: 120
                },
                {
                    showName: '实发数量',
                    columnName: 'realAmount',
                    columnWidth: 120
                },
                {
                    showName: '实发重量',
                    columnName: 'weight',
                    columnWidth: 120
                },
                {
                    showName: '出库状态',
                    columnName: 'handOutStatus',
                    columnWidth: 120,
                    showOption: [
                        {
                            optionKey: '0',
                            optionShow: '新建'
                        },{
                            optionKey: '1',
                            optionShow: '出库中'
                        },{
                            optionKey: '2',
                            optionShow: '出库完成'
                        },{
                            optionKey: '3',
                            optionShow: '审核通过'                            
                        }
                        // ,{
                        //     optionKey: '4',
                        //     optionShow: '审核不通过'                            
                        // }
                    ]
                },
                {
                    showName: '货主',
                    columnName: 'userDefined1',
                    showOption: []
                },
                {
                    showName: '车牌',
                    columnName: 'userDefined2'
                },
                {
                    showName: '箱号',
                    columnName: 'userDefined3'
                },
                {
                    showName: '单位',
                    columnName: 'unit'
                },
                {
                    showName: '计划日期',
                    columnName: 'outPlanTime',
                    columnWidth: 120
                },
                {
                    showName: '扫描开始时间',
                    columnName: 'scanTimeBegin',
                    columnWidth: 160
                },
                {
                    showName: '扫描结束时间',
                    columnName: 'scanTimeEnd',
                    columnWidth: 160
                },
                {
                    showName: '创建人',
                    columnName: 'createBy'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth: 140
                },{
                    showName: '最后修改人',
                    columnName: 'lastModifiedBy',
                    columnWidth: 120
                },{
                    showName: '最后修改时间',
                    columnName: 'gmtModified',
                    columnWidth: 160
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            // {
            //     showName: '型号类型',
            //     name: 'modelType',
            //     type: 'select',
            //     optionList: [//下拉框的信息
            //     ]
            // },
            {
                showName: '等级',
                name: 'grade',
                type: 'select',
                optionList: [//下拉框的信息
                      {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'一等品'
                    },{
                        optionKey:'2',
                        optionShow:'二等品'
                    },{
                        optionKey:'3',
                        optionShow:'处理品'
                    }
                ]
            },
            {
                showName: '级别',
                name: 'level',
                type: 'select',
                optionList: [//下拉框的信息
                      {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'A',
                        optionShow:'A'
                    },{
                        optionKey:'B',
                        optionShow:'B'
                    },{
                        optionKey:'C',
                        optionShow:'C'
                    },{
                        optionKey:'D',
                        optionShow:'D'
                    }
                ]
            },{
            //     showName: '类型',
            //     name: 'type',
            //     type: 'select',
            //     optionList: [//下拉框的信息
            //           {
            //             optionKey:'',
            //             optionShow:''
            //         },{
            //             optionKey:'0',
            //             optionShow:'正品'
            //         },{
            //             optionKey:'1',
            //             optionShow:'样品'
            //         }
            //     ]
            // },{
                showName: '仓库',
                name: 'waCode',
                type: 'select',
                optionList: []
                // url: '/base/data/area/get/wa_code'
            },{
                showName:'计划日期',
                name:'outPlanTime',
                type: 'date',
                maxName:'outPlanTimeMax',
                minName:'outPlanTimeMin'
            },{
                showName: '需求数量',
                name:'requireAmount',
                type: 'number',
                maxName:'requireAmountMax', //最大值
                minName:'requireAmountMin'  //最小值
            },{
                showName:'实发数量',
                name:'realAmount',
                type:'number',
                maxName:'realAmountMax',
                minName:'realAmountMin'
            },{
                showName:'实发重量',
                name:'weight',
                type:'number',
                maxName:'weightMax',
                minName:'weightMin'
            },
            {
                showName: '单位',
                name: 'unit',
                type: 'select',
                url:'/base/dic/get/unit'
            },
            {
                showName: '出库状态',
                name: 'handOutStatus',
                type: 'select',
                optionList: [
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '新建'
                    }, {
                        optionKey: '1',
                        optionShow: '出库中'
                    }, {
                        optionKey: '2',
                        optionShow: '出库完成'
                    },{
                        optionKey: '3',
                        optionShow: '审核通过'                            
                    }
                    // ,{
                    //     optionKey: '4',
                    //     optionShow: '审核不通过'                            
                    // }
                ]
            },{
                showName: '货主',
                name: 'userDefined1',
                type: 'select',
                optionList: []
            },
            {
                showName:'扫描开始时间',
                name:'scanTimeBegin',
                type: 'date',
                maxName:'scanTimeBeginMax',
                minName:'scanTimeBeginMin',
                showTime: '1'
            },{
                showName:'扫描结束时间',
                name:'scanTimeEnd',
                type: 'date',
                maxName:'scanTimeEndMax',
                minName:'scanTimeEndMin',
                showTime: '1'
            },
            {
                showName:'创建时间',
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime: '1'
            },{
                showName:'最后修改时间',
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin',
                showTime: '1'
            }
        ];

    }

    componentWillMount() {
        const options= {
            url: '/base/data/purchase/getPurchase',
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.purchaseInitCallBackHandle);

        const waCodeOptions = {
            url: '/base/data/area/get/wa_code',
            type: 'get'
        }
        fetchData(this, waCodeOptions, this.waCodeInitCallbackHandle);
    }
    //下拉框 初始化
    purchaseInitCallBackHandle = (resData)=>{
        // alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.purchaseCode;
            tmp.optionShow = e.purchaseName;
            optionList.push(tmp);
        });
        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[13].showOption = optionList;
        this.searchComponentData[9].optionList = optionList;
    }
    waCodeInitCallbackHandle = (resData)=>{
        // alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });
        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[8].showOption = optionList;
        this.searchComponentData[2].optionList = optionList;
    }
    // 行操作
    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'exp'){
            // this.expHandle(rowObj);
        }else if(strHandles === 'add'){
            this.addHandle(rowObj);
        }else if(strHandles == 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles == 'check'){
            this.checkHandle(rowObj);
        }else if(strHandles == 'checkAlone'){
            this.checkAloneHandle(rowObj);
        }else if(strHandles == 'print'){
            this.printHandle(rowObj);
        }else if(strHandles == 'editNo'){
            this.editNoHandle(rowObj);
        }else if(strHandles == 'toSell'){
            this.toSellHandle(rowObj);
        }
    }

    toSellHandle = (rowObj) =>{
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.setState({
                visible: true,
                formTitle: '转销售'
                // rowData: rowObj[0].data
            });
            const options = {
                url: '/product/hand/toSell',
                condition: {
                    sellOutNo: rowObj[0].data.handOutNo
                }
            };

            this.refreshHandle();
            console.log("------------转销售操作------------------")
            let _that = this;
            confirm({
                title: '您确定要转销售出库吗？',
                onOk() {
                    
                    fetchData(_that, options, (res)=>{
                        if (res.code == '1') {
                            message.success('转销售操作成功！');
                            _that.refreshHandle();
                        } else {
                            message.info(res.msg);
                        }
                    });
                    console.log("------------转手工操作------------------")
                },
                onCancel() {
                    console.log("取消了转手工操作")
                },
            });
        }
    };

    checkHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.checkPage.checkHandle(rowObj);
        }
    };
    checkAloneHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.checkAlonePage.checkAloneHandle(rowObj);
        }
    };

    printHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else if(rowObj[0].data.handOutStatus != "2"){
            Modal.error({
                title: '错误提示',
                content: '该出库单未出库完成',
            });
        }else{
            this.refs.printPage.printHandle(rowObj);
        }
    };

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updatePage.editHandle(rowObj);
        }
    };
    editNoHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updatePageNo.editNoHandle(rowObj);
        }
    };


    //按照库位新增
    addHandle = () => {
        this.refs.addPage.addHandle();
    };

    //刷新列表
    refreshHandle = () => {
        this.refs.proHandOutList.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'proHandOutList'
                    title="手工出库查询"
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/product/hand/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />
                <AddPage
                    ref="addPage"
                    refreshHandle = {this.refreshHandle}
                />
                <UpdatePage
                    ref="updatePage"
                    refreshHandle = {this.refreshHandle}
                />
                <UpdatePageNo
                    ref="updatePageNo"
                    refreshHandle = {this.refreshHandle}
                />

                <CheckPage
                    ref="checkPage"
                    refreshHandle = {this.refreshHandle}
                />  
                <CheckAlonePage
                    ref="checkAlonePage"
                    refreshHandle = {this.refreshHandle}
                />
                <PrintPage
                    ref="printPage"
                    refreshHandle = {this.refreshHandle}
                />   
            </div>
        )
        
    }

}